<template>
  <view>
    <view @click="gotoSearch('')"
      ><van-search
        :value="query"
        shape="round"
        background="#78cdd1"
        placeholder="搜索"
    />
	</view>
    <scroll-view
      @scrolltolower="scrolltolower"
      scroll-y="true"
      style="height: 1200rpx"
    >
      <view class="category-list">
        <view
          class="category"
          v-for="(row, index) in categoryList"
          :key="index"
          @click="gotoSearch(row.name)"
        >
          <view class="img"><image :src="row.img"></image></view>
          <view class="text">{{ row.name }}</view>
        </view>
      </view>
      <view class="goodsList">
        <!-- <goodsItem v-for="item in data.rows" :key="item._id" :goods="item" class="goodsItem"></goodsItem> -->
        <view class="listLeft"
          ><goodsItem
            v-for="(item, index) in data.rows"
            :key="item._id"
            v-if="(index + 1) % 2 != 0"
            :goods="item"
            class="goodsItem"
          ></goodsItem
        ></view>
        <view class="listRight"
          ><goodsItem
            v-for="(item, index) in data.rows"
            :key="item._id"
            v-if="(index + 1) % 2 == 0"
            :goods="item"
            class="goodsItem"
          ></goodsItem
        ></view>
      </view>
      <view class="showInfo" v-if="data.total == data.rows.length"
        >已经到底了</view
      >
    </scroll-view>
  </view>
</template>

<script>
import swiper from "../../components/swiper/index";
import goodsApi from "../../api/goods";
import goodsItem from "../../components/goodsItem/index";
export default {
  data() {
    return {
      data: {
        page: 1,
        limit: 5,
        total: 0,
        rows: [],
        query: {
          accurate: {
            check: 1,
            status: true,
          },
          vague: [{}],
        },
      },
      categoryList: [
        { id: 1, name: "主机", img: "/static/icon/zj.png" },
        { id: 2, name: "显卡", img: "/static/icon/xk.png" },
        { id: 3, name: "CPU", img: "/static/icon/cpu.png" },
        { id: 4, name: "更多", img: "/static/icon/more.png" },
      ],
    };
  },
  async created() {
    await this.getGoods();
    console.log(this.data);
  },
  components: {
    swiper,
    goodsItem,
  },
  methods: {
    gotoSearch(query) {
      if (query === "更多") {
        uni.switchTab({
          url: "/pages/fenLei/fenLei",
        });
        return;
      }
      uni.navigateTo({
        url: "/pages/search/search?search=" + query,
      });
    },
    getGoods() {
      goodsApi.get({
        page: this.data.page,
        limit: this.data.limit,
        query: this.data.query,
        cb: ({ data }) => {
          Object.assign(this.data, data);
        },
      });
    },
    scrolltolower() {
      if (this.data.total > this.data.rows.length) {
        this.data.page++;
        goodsApi.get({
          page: this.data.page,
          limit: this.data.limit,
          query: this.data.query,
          cb: ({ data }) => {
            this.data.rows.push(...data.rows);
          },
        });
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.goodsList {
  padding: 4%;
  display: flex;
  justify-content: space-between;
  .listLeft {
    width: 44%;
    display: flex;
    flex-direction: column;
    margin-right: 4%;
  }
  .listRight {
    width: 44%;
    display: flex;
    flex-direction: column;
  }
}
.showInfo {
  padding: 10rpx 0;
  margin: 0 4%;
  border-top: 1px solid #78cdd1;
  text-align: center;
  font-size: 24rpx;
  color: #aaa;
}
.category-list {
  width: 92%;
  margin: 0 4%;
  padding: 0 0 30rpx 0;
  border-bottom: solid 2rpx #78cdd1;
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  .category {
    width: 25%;
    margin-top: 50rpx;
    display: flex;
    flex-wrap: wrap;
    .img {
      width: 100%;
      display: flex;
      justify-content: center;
      image {
        width: 9vw;
        height: 9vw;
      }
    }
    .text {
      margin-top: 16rpx;
      width: 100%;
      display: flex;
      justify-content: center;
      font-size: 24rpx;
      color: #3c3c3c;
    }
  }
}
</style>
